package com.reinto.wallpaper.test;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;

public class WallpaperScreen extends AbstractScreen implements InputProcessor {
    private Texture splashTexture; // textura uvodni obrazovky
    private Texture svojdaTexture;
    private Image svojdaImage;

    float xpos;

    public WallpaperScreen(TestWallpaper game) { super(game); }

    @Override
    public void show() {
        super.show();

        // vytvorim texturu pro uvodni obrazovku
        splashTexture = new Texture("wallpaper.png");
        splashTexture.setFilter( Texture.TextureFilter.Linear, Texture.TextureFilter.Linear );
        svojdaTexture = new Texture("svojda.png");
        svojdaTexture.setFilter( Texture.TextureFilter.Linear, Texture.TextureFilter.Linear );

        //TextureRegion splashRegion = new TextureRegion(splashTexture,0,0,480,800);
        // vytvorime herce
        Image splashImage = new Image(splashTexture);
        splashImage.setWidth(480 * wRatio);
        splashImage.setHeight(800 * hRatio);
        splashImage.setX(0);
        splashImage.setY(0);

        stage.addActor(splashImage);

        svojdaImage = new Image(svojdaTexture);
        svojdaImage.setWidth(240 * wRatio);
        svojdaImage.setHeight(247 * hRatio);
        svojdaImage.setX((480/2 - 240/2) * wRatio);
        svojdaImage.setY(126 * hRatio);
        svojdaImage.setOrigin(240/2, 247/2);

        stage.addActor(svojdaImage);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render(float delta) {
        super.render(delta);

        if(svojdaImage.getX() == xpos) { svojdaImage.clearActions(); svojdaImage.addAction(rotateTo(0)); }
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
    }

    @Override
    public void dispose()
    {
        super.dispose();
        splashTexture.dispose();
    }

    @Override
    public boolean keyDown (int keycode) {
        return false;
    }

    @Override
    public boolean keyUp (int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped (char character) {
        return false;
    }

    @Override
    public boolean touchDown (int x, int y, int pointer, int newParam) {
        svojdaImage.clearActions();
        float pos = x;
        float time = Math.abs(svojdaImage.getX() - x) / 240;

        if(x > Gdx.graphics.getWidth() - svojdaImage.getWidth()) { pos = Gdx.graphics.getWidth() - svojdaImage.getWidth(); }
        svojdaImage.addAction(parallel(sequence(moveTo(pos, 126, time * 1.5f)), repeat(20, sequence(rotateBy(5, 0.1f), rotateBy(-10, 0.2f), rotateBy(5, 0.1f)))));

        xpos = x;
        if(xpos > Gdx.graphics.getWidth() - svojdaImage.getWidth()) { xpos = Gdx.graphics.getWidth() - svojdaImage.getWidth(); }
        return false;
    }

    @Override
    public boolean touchUp (int x, int y, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged (int x, int y, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved (int x, int y) {
        return false;
    }

    @Override
    public boolean scrolled (int amount) {
        return false;
    }
}
